package chtp02;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main2 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n=input.nextInt();
		String[] id = new String[100];
		for(int j=0;j<n;j++){
			id[j] = input.next();
		}
		//ArrayList str = new ArrayList(); 
		ArrayList<String> str = new ArrayList<String>();
	//	while(true) {
			String ch=input.next();
			while(ch.contains("sort1")||ch.contains("sort2")) {
				str.add(ch);
				 ch=input.next();
			}
			for (int i = 0; i <str.size(); i++) {
				switch (str.get(i)) {
				case "sort1":
					String[] year = new String[n];
					String[] month = new String[n];
					String[] date = new String[n];
					String[] sum = new String[n];
					
					for(int i1=0;i1<n;i1++) {
						  year[i1]=id[i1].substring(6, 10);
						month[i1]=id[i1].substring(10, 12);
						date[i1]=id[i1].substring(12, 14);
						sum[i1]=year[i1]+"-"+month[i1]+"-"+date[i1];
					}
					Arrays.sort(sum);
					for (int j = 0; j < sum.length; j++) {
						System.out.println(sum[j]);
					}
					break;
				case "sort2":
					String[] number=new String[n];
					for (int j = 0; j < n; j++) {
						number[j]=id[j].substring(6,14);
					}
					
					for(int i1=0;i1<n;i1++){
						for(int j=0;j<n-1;j++){
							//String head=number[j];
							//String next=number[j+1];
							int head=Integer.parseInt(number[j]);
							int next=Integer.parseInt(number[j+1]);
							if(head>next) {
								String temp=number[j];
								number[j]=number[j+1];
								number[j+1]=temp;
								String temp1=id[j];
								id[j]=id[j+1];
								id[j+1]=temp1;
						}
						}
						}
					
					for(int i1=0;i1<n;i1++){
						System.out.println(id[i1]);
					}
					break;

					
					}	
				
				
				}
			
			System.out.println("exit");
				

					
		}
	
	
}
